package api.data.controller;

import api.data.utils.DesUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.http.*;
import api.data.utils.JSONResult;

//http://localhost:8848/nacos/v1/ns/instance/list?serviceName=data-service

@RestController
@Slf4j
public class ApiController {
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL; //服务提供者的服务名

    @GetMapping(value = "/login/{username}/{password}")
    public JSONResult login(@PathVariable String username,@PathVariable String password){
        System.out.println(serverURL + "/query/users/select|*/where|username=@"+username+"|password=@"+password+"@");
        String rs = restTemplate.getForObject(serverURL + "/query/users/select|*/where|username=@"+username+"@|password=@"+password+"@", String.class);
        JSONObject obj = (JSONObject) JSON.parse(rs);
        System.out.println(obj);
        JSONArray al = (JSONArray) obj.get("data");
        System.out.println(al);
        if(al.size() > 0){
            JSONObject user = (JSONObject) al.get(0);
            String usernameMd5 = DesUtils.encode(username);
            user.put("token",usernameMd5);
            user.put("userid",user.get("id"));
            return JSONResult.ok(1,user);
        }else{
            return JSONResult.ok(0,new JSONObject());
        }
    }

    @GetMapping(value = "/gets/{tablename}")
    public JSONResult gets(@PathVariable String tablename){
        String rs = restTemplate.getForObject(serverURL + "/gets/" + tablename, String.class);
        return JSONResult.format(rs);
    }

    @GetMapping("/get/{tablename}/{id}")
    public JSONResult getUsers(@PathVariable("tablename") String tablename,@PathVariable("id") Integer id) {
        String rs = restTemplate.getForObject(serverURL + "/get/" + tablename + "/" + id.toString() , String.class);
        return JSONResult.format(rs);
    }

    @PostMapping(value = "/add/{tablename}")
    public JSONResult add(@PathVariable String tablename,@RequestBody Map<String,Object> params){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json;charset=utf-8");
        HttpEntity httpEntity = new HttpEntity(params, headers);
        String rs = restTemplate.postForObject(serverURL + "/add/" + tablename, httpEntity, String.class);
        return JSONResult.format(rs);
    }

    @PostMapping(value = "/update/{tablename}/{id}")
    public JSONResult update(@PathVariable String tablename,@PathVariable Integer id,@RequestBody Map<String,Object> item){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json;charset=utf-8");
        HttpEntity httpEntity = new HttpEntity(item, headers);
        restTemplate.put(serverURL + "/update/" + tablename + "/" + id.toString(), httpEntity);
        return JSONResult.ok();
    }

    @PostMapping(value = "/delete/{tablename}/{id}")
    public JSONResult delete(@PathVariable String tablename,@PathVariable Integer id){
        restTemplate.delete(serverURL + "/delete/" + tablename + "/" + id.toString());
        return JSONResult.ok();
    }

    @GetMapping(value = "/getslimit/{tablename}/{begin}/{count}")
    public JSONResult getlimit(@PathVariable String tablename,@PathVariable Integer begin,@PathVariable Integer count){
        String rs = restTemplate.getForObject(serverURL + "/getslimit/" + tablename + "/" + begin.toString() + "/" + count.toString(), String.class);
        return JSONResult.format(rs);
    }

    @GetMapping(value = "/query/{tablename}/{selectreg}/{wherereg}")
    public JSONResult query(@PathVariable String tablename,@PathVariable String selectreg,@PathVariable String wherereg){
        selectreg = selectreg.replace("||",",");
        wherereg = wherereg.replace("|||","%20");
        System.out.println(selectreg);
        System.out.println(wherereg);
        String rs = restTemplate.getForObject(serverURL + "/query/" + tablename + "/" + selectreg.toString() + "/" + wherereg.toString(), String.class);
        return JSONResult.format(rs);
    }

    @GetMapping(value = "/querylimit/{tablename}/{selectreg}/{wherereg}/{begin}/{count}")
    public JSONResult querylimit(@PathVariable String tablename,@PathVariable String selectreg,@PathVariable String wherereg,@PathVariable Integer begin,@PathVariable Integer count){
        selectreg = selectreg.replace("||",",");
        wherereg = wherereg.replace("|||","%20");
        System.out.println(selectreg);
        System.out.println(wherereg);
        String rs = restTemplate.getForObject(serverURL + "/querylimit/" + tablename + "/" + selectreg + "/" + wherereg + "/" + begin.toString() + "/" + count.toString(), String.class);
        return JSONResult.format(rs);
    }

    @GetMapping(value = "/get_structure/{tablename}")
    public JSONResult getStructure(@PathVariable String tablename){
        String rs = restTemplate.getForObject(serverURL + "/get_structure/" + tablename, String.class);
        return JSONResult.format(rs);
    }

    @GetMapping(value = "/get_tree/{tablename}/{id}/{level}")
    public JSONResult getTree(@PathVariable String tablename,@PathVariable Integer id,@PathVariable String level){
        String rs = restTemplate.getForObject(serverURL + "/get_tree/" + tablename + "/" + id.toString() + "/" + level.toString(), String.class);
        return JSONResult.format(rs);
    }
}

